#include "EchoServer.h"
#include <unistd.h>
#include <sys/syscall.h> 
#include <iostream>

// 构造函数
EchoServer::EchoServer(const std::string &ip, const uint16_t port,int subthreadnum,int workthreadnum)
    : tcpserver_(ip, port,subthreadnum),threadpool_(workthreadnum,"WORKS")
{
    // 构造函数体（可选）
    // TCP服务器回调设置（深色背景/白字/语法高亮）
    tcpserver_.setNewConnectionCB(std::bind(&EchoServer::HandleNewConnection,
                                            this,
                                            std::placeholders::_1));

    tcpserver_.setCloseConnectionCB(std::bind(&EchoServer::HandleClose,
                                              this,
                                              std::placeholders::_1));

    tcpserver_.setErrorConnectionCB(std::bind(&EchoServer::HandleError,
                                              this,
                                              std::placeholders::_1));

    tcpserver_.setOnMessageCB(std::bind(&EchoServer::HandleMessage,
                                        this,
                                        std::placeholders::_1,
                                        std::placeholders::_2));

    tcpserver_.setSendCompleteCB(std::bind(&EchoServer::HandleSendComplete,
                                           this,
                                           std::placeholders::_1));

    tcpserver_.setTimeoutCB(std::bind(&EchoServer::HandleTimeout,
                                      this,
                                      std::placeholders::_1));
}

// 析构函数
EchoServer::~EchoServer()
{
    // 析构函数体（可选）
}

// 启动服务器
void EchoServer::Start()
{
    // 启动逻辑
    tcpserver_.start();
}

// 新连接回调
void EchoServer::HandleNewConnection(std::shared_ptr<Connection> conn)
{
    // 新连接处理逻辑
    std::cout << "echoserver new connection come in" << std::endl;
    printf("EchoServer::HandleNewConnection() thread is: %d\n", syscall(SYS_gettid));
}

// 关闭连接回调
void EchoServer::HandleClose(std::shared_ptr<Connection> conn)
{
    // 关闭连接处理逻辑
    std::cout << "echoserver connection closed" << std::endl;
}

// 错误回调
void EchoServer::HandleError(std::shared_ptr<Connection> conn)
{
    // 错误处理逻辑
    std::cout << "echoserver error" << std::endl;
}

// 接收消息回调
void EchoServer::HandleMessage(std::shared_ptr<Connection> conn, std::string& message)
{
    // printf("EchoServer::HandleMessage() thread is: %d\n", syscall(SYS_gettid));
    // fflush(stdout);
    // 消息处理逻辑
    //message = "replay:" + message; // 回显业务
    //conn->send(message.data(), message.size());
    threadpool_.addtask(std::bind(&EchoServer::OnMessage,this,conn,message));

}

// 发送完成回调
void EchoServer::HandleSendComplete(std::shared_ptr<Connection> conn)
{
    // 发送完成处理逻辑
    std::cout << "echoserver sendcomplete" << std::endl;
}

// 超时回调
void EchoServer::HandleTimeout(EventLoop *loop)
{
    // 超时处理逻辑
    std::cout << "echoserver timeout" << std::endl;
}


void EchoServer::OnMessage(std::shared_ptr<Connection> conn, std::string& message)
{
    // printf("work thread is: %d\n", syscall(SYS_gettid));
    // std::cout<<"work thread is:"<<syscall(SYS_gettid)<<std::endl;
    message = "replay:" + message; // 回显业务
    conn->send(message.data(), message.size());
}